const { JSDOM } = require("jsdom");

const ListMy = ({ proxyRes, res, response }) => {
  if (proxyRes.statusCode !== 200) {
    res.status(200);
    return JSON.stringify({
      success: false,
    });
  }

  res.setHeader("Content-Type", "application/json");

  try {
    const dom = new JSDOM(response);
    const document = dom.window.document;
    const List = [];

    const domList = [...document.querySelectorAll(".buy_table_gray")];

    domList.forEach((tr) => {
      const [td1, td2, td3, td4, td5, td6] = [...tr.querySelectorAll("td")];
      const orderCode = [...td5.querySelectorAll('span')].map(item=>item.innerHTML).filter((item) => item);
  

      List.push({
        id: orderCode,
        name: td1.textContent,
        orderName: td2.textContent,
        price: td3.textContent,
        date: td4.textContent,
        orderCode,
        status: td6.textContent,
      });
    });

    return JSON.stringify({
      data: List,
      success: true,
    });
  } catch (error) {
  console.log(error);
    return JSON.stringify({
      success: false,
    });
  }
};

module.exports = {
  ListMy,
};
